#!/usr/bin/env python

import urllib2
import urllib
import socket
import sys

AE2_URL = "https://ae.utbm.fr/streaminfo.php"

def make_postdata(args, key):
    # Kill the last argument if we don't have args in pairs.
    if len(args) % 2:
        args.pop()
    keyval_pairs = zip(args[::2], args[1::2])
    keyval_pairs.append(('key', key))
    return urllib.urlencode(keyval_pairs)

def main():
    socket.setdefaulttimeout(3.0)
    try:
        key, args = sys.argv[1], sys.argv[2:]
        f = urllib2.urlopen(AE2_URL, make_postdata(args, key))
        result = f.read()
        f.close()
        print result
    except urllib2.URLError:
        print "Could not connect."

if __name__ == '__main__':
    main()
